#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件 ：model.py
@作者 : 
@时间 : 2022/8/30 10:33
@版本 : 1.0
说明  ： 
'''
import datetime
from sqlalchemy.orm import sessionmaker,scoped_session
from sqlalchemy.ext.declarative import declarative_base
from .extension import db_engine
from loguru import logger
from sqlalchemy import (
    Column,
    Integer,
    String,
    Enum,
    DECIMAL,
    DateTime,
    Boolean,
    UniqueConstraint,
    Index,
    VARCHAR,
    Text
)

# 基础类
Base = declarative_base()
# 创建引擎

# 绑定引擎
Session = sessionmaker(bind=db_engine)
# 创建数据库链接池，直接使用session即可为当前线程拿出一个链接对象conn
# 内部会采用threading.local进行隔离
session = scoped_session(Session)
class ORM_Access_Logs(Base):
    #表名称
    __tablename__='access_logs'
    __table_args__ = {"schema": 'sanic'}
    id = Column('id', Integer, primary_key=True, autoincrement=True)
    access_host = Column('access_host', String(length=100), nullable=True)
    access_url = Column('access_url', VARCHAR(length=500), nullable=True)
    access_method = Column('access_method', VARCHAR(length=100), nullable=True)
    access_params_info = Column('access_params_info', Text(), nullable=True)
    access_time = Column('access_time', DateTime(), nullable=True)
    create_time = Column('create_time', DateTime(), nullable=True)

    # 创建数据库表
    @staticmethod
    def create_database_table():
        # Base.metadata.create_all(sqlite_engine)
        Base.metadata.create_all(db_engine)
        logger.info('CREATE TABLE {} SUCCESS'.format(ORM_Ifa_Data_Ext_Access_Logs.__tablename__))
        # print('CREATE TABLE T_TBK_GOODS SUCCESS')
